#include "my_ui_api.h"
#include "screens.h"
#include "styles.h"
#include "fatfs_driver.h"
#include "szp_hardware_driver.h"
#include "ff.h"
#include "esp_camera.h"
#include <dirent.h>
#include <stdio.h>

static const char *TAG = "my_ui_api";

#define MAX_FILES 50           // 根据实际情况调整
char file_list[MAX_FILES][64]; // 保存文件名的数组（假设文件名不超过64字符）
int file_count = 0;

extern void wifi_select_cb(lv_event_t *e);
extern void picture_select_cb(lv_event_t *e);
void add_wifi_panel(int16_t signal, char *wifi_name, bool has_passssword)
{
    extern lv_img_dsc_t img_wi_fi_1_32, img_wi_fi_2_32, img_wi_fi_3_32, img_wi_fi_4_32, img_lock, img_turn_right;
    lv_obj_t *parent_obj = objects.wifi_list;
    {
        lv_obj_t *obj = lv_obj_create(parent_obj);
        lv_obj_set_pos(obj, 36, 75);
        lv_obj_set_size(obj, 250, 41);
        lv_obj_set_scrollbar_mode(obj, LV_SCROLLBAR_MODE_OFF);
        lv_obj_set_scroll_dir(obj, LV_DIR_NONE);
        lv_obj_set_scroll_snap_x(obj, LV_SCROLL_SNAP_NONE);
        lv_obj_set_scroll_snap_y(obj, LV_SCROLL_SNAP_NONE);
        lv_obj_set_style_border_width(obj, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_obj_set_style_bg_color(obj, lv_color_hex(0xfff5f5f5), LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_obj_set_style_bg_color(obj, lv_color_hex(0xffd3d3d3), LV_PART_MAIN | LV_STATE_PRESSED);
        char *wifi_name_copy = lv_mem_alloc(strlen(wifi_name) + 1);
        strcpy(wifi_name_copy, wifi_name);
        lv_obj_add_event_cb(obj, wifi_select_cb, LV_EVENT_ALL, (void *)wifi_name_copy);
        {
            lv_obj_t *parent_obj = obj;
            {
                lv_obj_t *obj = lv_img_create(parent_obj);
                lv_obj_set_pos(obj, -3, -10);
                lv_obj_set_size(obj, 30, 30);
                if (signal > -50)
                {
                    lv_img_set_src(obj, &img_wi_fi_1_32);
                }
                else if (signal > -70)
                {
                    lv_img_set_src(obj, &img_wi_fi_2_32);
                }
                else if (signal > -80)
                {
                    lv_img_set_src(obj, &img_wi_fi_3_32);
                }
                else
                {
                    lv_img_set_src(obj, &img_wi_fi_4_32);
                }
                lv_img_set_zoom(obj, 200);
            }
            {
                lv_obj_t *obj = lv_label_create(parent_obj);
                // lv_obj_set_pos(obj, 0, 0);
                lv_obj_set_pos(obj, -13, 13);
                lv_obj_set_size(obj, 120, 41);
                // lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
                lv_label_set_text(obj, wifi_name);
                lv_label_set_long_mode(obj, LV_LABEL_LONG_SCROLL_CIRCULAR);
                add_style_default_lab(obj);
            }
            if (has_passssword)
            {
                lv_obj_t *obj = lv_img_create(parent_obj);
                lv_obj_set_pos(obj, 167, -9);
                lv_obj_set_size(obj, 30, 30);
                lv_img_set_src(obj, &img_lock);
                lv_img_set_zoom(obj, 150);
                lv_obj_set_style_img_opa(obj, 200, LV_PART_MAIN | LV_STATE_DEFAULT);
                lv_obj_set_style_img_recolor(obj, lv_color_hex(0xff000000), LV_PART_MAIN | LV_STATE_DEFAULT);
            }
            {
                lv_obj_t *obj = lv_img_create(parent_obj);
                lv_obj_set_pos(obj, 200, -9);
                lv_obj_set_size(obj, 30, 30);
                lv_img_set_src(obj, &img_turn_right);
                lv_img_set_zoom(obj, 150);
            }
        }
    }
}

void add_jpg_dir(char *jpg_name)
{
    lv_obj_t *btn;
    btn = lv_list_add_btn(objects.picture_list, NULL, jpg_name);
    char *jpg_name_copy = lv_mem_alloc(strlen(jpg_name) + 1);
    strcpy(jpg_name_copy, jpg_name);
    lv_obj_add_event_cb(btn, picture_select_cb, LV_EVENT_CLICKED, (void *)jpg_name_copy);
    lv_group_remove_obj(btn);
}

lv_obj_t *spinner;

void show_spinner()
{
    if (spinner == NULL)
    {
        ESP_LOGI(TAG, "create spinner");
        lvgl_port_lock(0);
        spinner = lv_spinner_create(lv_scr_act(), 1000, 60);
        lv_obj_set_pos(spinner, 135, 95);
        lv_obj_set_size(spinner, 50, 50);
        lv_obj_set_style_arc_opa(spinner, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_obj_set_style_arc_color(spinner, lv_color_hex(0xff767676), LV_PART_INDICATOR | LV_STATE_DEFAULT);
        lv_obj_set_style_arc_width(spinner, 10, LV_PART_INDICATOR | LV_STATE_DEFAULT);
        lv_obj_set_style_arc_opa(spinner, 150, LV_PART_INDICATOR | LV_STATE_DEFAULT);
        lv_obj_set_style_arc_rounded(spinner, false, LV_PART_INDICATOR | LV_STATE_DEFAULT);
        lv_obj_set_style_radius(spinner, 9, LV_PART_INDICATOR | LV_STATE_DEFAULT);
        lvgl_port_unlock();
    }
    else
    {
        ESP_LOGI(TAG, "spinner had been create");
    }
}

void close_spinner()
{
    if (spinner != NULL)
    {
        ESP_LOGI(TAG, "delete spinner");
        lvgl_port_lock(0);
        lv_obj_del(spinner);
        lvgl_port_unlock();
        spinner = NULL;
    }
    else
        ESP_LOGI(TAG, "spinner had been delete");
}

void frush_picture_list()
{
    lv_obj_clean(objects.picture_list);
    struct dirent *dir;
    DIR *dirp;
    int ret = 0;

    /* 打开目录 */
    dirp = opendir(MOUNT_POINT);
    if (NULL == dirp)
    {
        perror("opendir error");
        exit(-1);
    }

    /* 循环读取目录流中的所有目录条目 */
    while (NULL != (dir = readdir(dirp)))
    {
        char *dot = strrchr(dir->d_name, '.'); // 查找最后一个点号
        if (dot != NULL && (strcasecmp(dot, ".jpg") == 0))
        {
            ESP_LOGI(TAG, "Found JPG file: %s", dir->d_name);
            add_jpg_dir(dir->d_name);
        }
    }

    closedir(dirp);
}

static lv_obj_t *delete_button = NULL, *close_button = NULL, *img_jpg = NULL, *fille_name_label = NULL;

static void event_handler_cb_picture_show_close_button(lv_event_t *e)
{
    ESP_LOGI(TAG, "close button clicked");
    lv_obj_del(delete_button);
    lv_obj_del(close_button);
    lv_obj_del(img_jpg);
    delete_button = NULL;
    img_jpg = NULL;
    close_button = NULL;
    fille_name_label = NULL;
    eez_flow_set_screen(SCREEN_ID_PAGE_3, LV_SCR_LOAD_ANIM_FADE_ON, 200, 0);
}

static void ok_msgbox_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *msgbox = lv_event_get_current_target(e);
    const char *file_name = (const char *)lv_event_get_user_data(e);

    if (code == LV_EVENT_VALUE_CHANGED)
    {
        const char *txt = lv_msgbox_get_active_btn_text(msgbox);
        if (strcmp(txt, "OK") == 0)
        {
            lv_obj_del(delete_button);
            lv_obj_del(close_button);
            lv_obj_del(img_jpg);
            delete_button = NULL;
            img_jpg = NULL;
            close_button = NULL;
            fille_name_label = NULL;
            eez_flow_set_screen(SCREEN_ID_PAGE_3, LV_SCR_LOAD_ANIM_FADE_ON, 200, 0);
            lv_msgbox_close(msgbox);
            const char path[20];
            sprintf(path, "%s/%s", MOUNT_POINT, file_name);
            remove(path);
            frush_picture_list();
        }
    }
}

static void delete_msgbox_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *msgbox = lv_event_get_current_target(e);
    const char *file_name = (const char *)lv_event_get_user_data(e);

    if (code == LV_EVENT_VALUE_CHANGED)
    {
        const char *txt = lv_msgbox_get_active_btn_text(msgbox);
        if (strcmp(txt, "确定") == 0)
        {
            lv_msgbox_close(msgbox);
            {
                // static const char *btns[] = {"OK", ""};
                // lv_obj_t *mbox = lv_msgbox_create(lv_scr_act(), "", "照片删除成功", btns, false);
                // lv_obj_add_event_cb(mbox, ok_msgbox_event_cb, LV_EVENT_ALL, (void *)file_name);
                // lv_group_focus_obj(lv_msgbox_get_btns(mbox));
                // lv_obj_add_state(lv_msgbox_get_btns(mbox), LV_STATE_FOCUS_KEY);

                // lv_obj_align(mbox, LV_ALIGN_CENTER, 0, 0);

                // lv_obj_t *bg = lv_obj_get_parent(mbox);
                // lv_obj_set_style_bg_opa(bg, LV_OPA_70, 0);
                // lv_obj_set_style_bg_color(bg, lv_palette_main(LV_PALETTE_GREY), 0);

                // extern lv_font_t *ui_font_cn;
                // lv_obj_set_style_text_font(mbox, &ui_font_cn, LV_PART_MAIN | LV_STATE_DEFAULT);
                lv_obj_del(delete_button);
                lv_obj_del(close_button);
                lv_obj_del(img_jpg);
                delete_button = NULL;
                img_jpg = NULL;
                close_button = NULL;
                fille_name_label = NULL;
                eez_flow_set_screen(SCREEN_ID_PAGE_3, LV_SCR_LOAD_ANIM_FADE_ON, 200, 0);
                const char path[20];
                sprintf(path, "%s/%s", MOUNT_POINT, file_name);
                remove(path);
                frush_picture_list();
            }
        }
        else if (strcmp(txt, "取消") == 0)
        {
            lv_msgbox_close(msgbox);
        }
    }
}

static void msgbox_create(char *jpg_name)
{
    static const char *btns[] = {"确定", "取消", ""};
    lv_obj_t *mbox = lv_msgbox_create(lv_scr_act(), "警告", "确定要删除此照片？", btns, false);
    char *jpg_name_copy = lv_mem_alloc(strlen(jpg_name) + 1);
    strcpy(jpg_name_copy, jpg_name);
    lv_obj_add_event_cb(mbox, delete_msgbox_event_cb, LV_EVENT_ALL, (void *)jpg_name_copy);
    lv_group_focus_obj(lv_msgbox_get_btns(mbox));
    lv_obj_add_state(lv_msgbox_get_btns(mbox), LV_STATE_FOCUS_KEY);

    lv_obj_align(mbox, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t *bg = lv_obj_get_parent(mbox);
    lv_obj_set_style_bg_opa(bg, LV_OPA_70, 0);
    lv_obj_set_style_bg_color(bg, lv_palette_main(LV_PALETTE_GREY), 0);
    extern lv_font_t *ui_font_cn;
    lv_obj_set_style_text_font(mbox, &ui_font_cn, LV_PART_MAIN | LV_STATE_DEFAULT);
}

static void event_handler_cb_picture_show_delete_button(lv_event_t *e)
{
    const char *file_name = (const char *)lv_event_get_user_data(e);
    ESP_LOGI(TAG, "click = %s", file_name);
    msgbox_create(file_name);
}

void show_picture(char *jpg_name)
{
    if (img_jpg == NULL)
    {
        const char path[20];
        sprintf(path, "S:/%s", jpg_name);
        img_jpg = lv_img_create(lv_scr_act());
        lv_img_set_src(img_jpg, path);
        lv_obj_center(img_jpg);
        {
            fille_name_label = lv_label_create(img_jpg);
            lv_obj_set_pos(fille_name_label, 0, 112);
            lv_obj_set_size(fille_name_label, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
            lv_label_set_text(fille_name_label, jpg_name);
            add_style_default_lab(fille_name_label);
            lv_obj_set_style_align(fille_name_label, LV_ALIGN_CENTER, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_text_color(fille_name_label, lv_color_hex(0xffbcbcbc), LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_text_font(fille_name_label, &lv_font_montserrat_14, LV_PART_MAIN | LV_STATE_DEFAULT);
        }
    }
    if (delete_button == NULL)
    {
        delete_button = lv_btn_create(img_jpg);
        lv_obj_set_pos(delete_button, 276, 0);
        lv_obj_set_size(delete_button, 42, 40);
        char *jpg_name_copy = lv_mem_alloc(strlen(jpg_name) + 1);
        strcpy(jpg_name_copy, jpg_name);
        lv_obj_add_event_cb(delete_button, event_handler_cb_picture_show_delete_button, LV_EVENT_CLICKED, (void *)jpg_name_copy);

        lv_obj_set_style_bg_opa(delete_button, 0, LV_PART_MAIN | LV_STATE_DEFAULT); // 背景透明度100%
        lv_obj_set_style_border_width(delete_button, 0, LV_PART_MAIN);              // 边框宽度归零
        lv_obj_set_style_shadow_width(delete_button, 0, LV_PART_MAIN);              // 关闭阴影
        lv_obj_set_style_outline_width(delete_button, 0, LV_PART_MAIN);             // 消除轮廓线
        lv_group_remove_obj(delete_button);
        {
            lv_obj_t *obj = lv_label_create(delete_button);
            lv_obj_set_pos(obj, 0, 0);
            lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
            lv_label_set_text(obj, LV_SYMBOL_TRASH);
            lv_obj_set_style_align(obj, LV_ALIGN_CENTER, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_text_color(obj, lv_color_hex(0xffbcbcbc), LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_text_font(obj, &lv_font_montserrat_20, LV_PART_MAIN | LV_STATE_DEFAULT);
        }
    }
    if (close_button == NULL)
    {
        close_button = lv_btn_create(img_jpg);
        lv_obj_set_pos(close_button, 1, 0);
        lv_obj_set_size(close_button, 42, 40);
        lv_obj_add_event_cb(close_button, event_handler_cb_picture_show_close_button, LV_EVENT_CLICKED, NULL);

        lv_obj_set_style_bg_opa(close_button, 0, LV_PART_MAIN | LV_STATE_DEFAULT); // 背景透明度100%
        lv_obj_set_style_border_width(close_button, 0, LV_PART_MAIN);              // 边框宽度归零
        lv_obj_set_style_shadow_width(close_button, 0, LV_PART_MAIN);              // 关闭阴影
        lv_obj_set_style_outline_width(close_button, 0, LV_PART_MAIN);             // 消除轮廓线
        lv_group_remove_obj(close_button);
        {
            lv_obj_t *obj = lv_label_create(close_button);
            lv_obj_set_pos(obj, 0, 0);
            lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
            lv_label_set_text(obj, LV_SYMBOL_CLOSE);
            lv_obj_set_style_align(obj, LV_ALIGN_CENTER, LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_text_color(obj, lv_color_hex(0xffbcbcbc), LV_PART_MAIN | LV_STATE_DEFAULT);
            lv_obj_set_style_text_font(obj, &lv_font_montserrat_20, LV_PART_MAIN | LV_STATE_DEFAULT);
        }
    }
}
